package cn.bevisoft.casex.biz.manager.impl;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import cn.bevisoft.casex.biz.manager.UserMananger;
import cn.bevisoft.casex.biz.manager.exception.UserNotFoundException;
import cn.bevisoft.casex.biz.manager.exception.PasswordIncorrectException;
import cn.bevisoft.casex.dal.dao.UserDAO;
import cn.bevisoft.casex.dal.model.User;

@Repository("userMananger")
public class UserManangerImpl implements UserMananger {
	
	private static final Logger log =  LoggerFactory.getLogger(UserManangerImpl.class);
	
	@Autowired
	private UserDAO userDAO;
	
	
	@Override
	public User getUserByUsernameAndPassword(String username, String password) {
		User user = new User();
		user.setLoginId("xubo.wuxb@alibaba-inc.com");
		user.setPassword("passwords1");
		
		User userdb = this.userDAO.selectUserByLoginIdAndPassword("xubo", "123");
		log.info("userdb="+userdb);
		
		log.info("return user ="+user);
		return user;
	}


	@Override
	public User login(String loginId, String password)
			throws UserNotFoundException, PasswordIncorrectException {
		
		//验证用户名是否存在
		User user = this.userDAO.selectUserByLoginId(loginId);
		if(user==null){
			throw new UserNotFoundException("user not exited in database:loginId="+loginId);
		}
		
		// 验证密码是否正确
		// TODO 如果数据密码是加密过的，那么则需要对用户输入的密码加密之后再比较
		if(!StringUtils.equalsIgnoreCase(user.getPassword(), password)){
			throw new PasswordIncorrectException("password incorrect:password=" + password);
		}
		
		return user;
	}

}
